package elijah.base.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import elijah.base.R;
import elijah.base.databinding.ViewClearEdittextBinding;

public class ClearEditText extends BaseWidget<ViewClearEdittextBinding> {
    private int paintColor = 0xff888888;
    private int backgroundMode;


    private final RectF rectF = new RectF();


    public ClearEditText(@NonNull Context context) {
        super(context);
    }

    public ClearEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    private Paint paint;

    @Override
    public void initView(@Nullable AttributeSet attrs) {
        setWillNotDraw(false);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(1f);
        paint.setStyle(Paint.Style.STROKE);


        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ClearEditText);
            binding.editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.ClearEditText_textSize, binding.editText.getTextSize()));
            binding.editText.setTextColor(typedArray.getColor(R.styleable.ClearEditText_textColor, binding.editText.getCurrentTextColor()));
            backgroundMode = typedArray.getInt(R.styleable.ClearEditText_backgroundMode, 0);
            binding.editText.setHint(typedArray.getString(R.styleable.ClearEditText_android_hint));
            binding.editText.setInputType(typedArray.getInt(R.styleable.ClearEditText_android_inputType, InputType.TYPE_CLASS_TEXT));
            Drawable icon = typedArray.getDrawable(R.styleable.ClearEditText_icon);
            if (icon != null) {
                binding.icon.setIcon(icon);
                binding.icon.setVisibility(VISIBLE);
            } else {
                binding.icon.setVisibility(GONE);
            }
            ColorStateList iconTint = typedArray.getColorStateList(R.styleable.ClearEditText_iconTint);
            if (iconTint != null) {
                binding.icon.setIconTint(iconTint);
            }

            typedArray.recycle();
            postInvalidate();
        }

        binding.clear.setOnClickListener(v -> {
            binding.editText.setText("");
            if (clearCallback != null) {
                clearCallback.onClear();
            }
        });

        binding.editText.setOnFocusChangeListener((view, b) -> {
            paintColor =
                    b ? getColorBackground() : 0xff888888;
            postInvalidate();

        });
        binding.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                binding.clear.setVisibility(TextUtils.isEmpty(s) ? INVISIBLE : VISIBLE);
            }
        });
    }

    private int getColorBackground() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
            int[] attrs = new int[]{android.R.attr.colorSecondary};
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs);
            int color = typedArray.getColor(0, 0xFF03DAC5);
            typedArray.recycle();
            return color;
        }
        return com.google.android.material.R.color.design_default_color_secondary;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(paintColor);
        if (backgroundMode == 0) {
            canvas.drawLine(0, getHeight() / 2f + binding.editText.getHeight() / 2f, getWidth(), getHeight() / 2f + binding.editText.getHeight() / 2f, paint);
        } else if (backgroundMode == 1) {
            rectF.set(4, getHeight() / 2f - binding.editText.getHeight() / 2f - 4, getWidth() - 4, getHeight() / 2f + binding.editText.getHeight() / 2f + 4);
            canvas.drawRoundRect(rectF, 4, 4, paint);

        }
    }

    public void setText(String text) {
        binding.editText.setText(text);
        binding.editText.setSelection(binding.editText.length());
    }

    public String getText() {
        return binding.editText.getText().toString();
    }

    public ViewClearEdittextBinding getBinding() {
        return binding;
    }


    private ClearCallback clearCallback;

    public ClearEditText setClearCallback(ClearCallback clearCallback) {
        this.clearCallback = clearCallback;
        return this;
    }

    public interface ClearCallback {
        void onClear();
    }

}
